
var _util = require('util/util.js');
const { getServerURL, request } = require('../util/util');
// service/catalog-service.js
var _catalog_service = {
    // 修改后的接口地址，注意后端映射为 /catalog/categorise/{id}
    getProductList: function (id, resolve, reject) {
        _util.request({
            url: _util.getServerURL('catalog/categorise/' + id),
            method: "GET",
            success: resolve,
            error: reject,
        });
    },
    
    getProductByCategoryId: function (categoryId, resolve, reject) {
        _util.request({
            url: _util.getServerURL('catalog/categories/' + categoryId),
            method: "GET",
            success: resolve,
            error: reject,
        });
    },
    // 修改后的接口地址，注意后端映射为 /catalog/categories/{id}/products
    getProductList: function (id, resolve, reject) {
        _util.request({
            url: _util.getServerURL('catalog/categories/' + id + '/products'),
            method: "GET",
            success: resolve,
            error: reject,

        });
    },

    // 修改后的接口地址，注意后端映射为 /catalog/products/{id}/items
    getItemList: function (id, resolve, reject) {
        _util.request({
            url: _util.getServerURL('catalog/products/' + id + '/items'),
            method: "GET",
            success: resolve,
            error: reject
        });
    },

    // 修改后的接口地址，注意后端映射为 /catalog/products/items/{id}
    getItem: function (id, resolve, reject) {
        _util.request({
            url: _util.getServerURL('catalog/products/items/' + id),
            method: "GET",
            success: resolve,
            error: reject
        });
    },

    // 修改后的接口地址，注意后端映射为 /catalog/search/products
    searchProduct: function (keyword, resolve, reject) {
        if (!keyword || keyword.trim() === "") {
            alert("搜索关键字不能为空！");
            return;
        }
        _util.request({
            url: _util.getServerURL('catalog/search_products'),
            data: JSON.stringify({ keyword: keyword.trim() }), // 确保关键字被正确包装为 JSON
            method: "POST",
            contentType: "application/json", // 设置 Content-Type 为 JSON
            success: resolve,
            error: reject
        });
    },
    getProduct: function (id, resolve, reject) {
        _util.request({
            url: _util.getServerURL('catalog/products/' + id),
            success: resolve,
            error: reject
        });
    }   

};
module.exports = _catalog_service;